package com.xiaobao.campusLostFoundAfter.controller;

import com.github.pagehelper.PageInfo;
import com.xiaobao.campusLostFoundAfter.common.Result;
import com.xiaobao.campusLostFoundAfter.pojo.Comment;
import com.xiaobao.campusLostFoundAfter.pojo.FoundItem;
import com.xiaobao.campusLostFoundAfter.service.FoundItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;


import java.util.*;

/*寻物控制模块*/
@RestController
@RequestMapping("/FoundItem")
public class FoundItemController {
    private static final Logger log = LoggerFactory.getLogger(FoundItemController.class);
    private final FoundItemService foundItemService;

    public FoundItemController(FoundItemService foundItemService) {
        this.foundItemService = foundItemService;
    }

    /**
     * 分页查询失物接口
     *
     * @param categoryId 分类ID，0表示所有分类
     * @param page       页码，从1开始
     * @param pageSize   每页记录数
     * @param keyword    搜索关键词
     * @return 分页查询结果
     */
    @GetMapping("/page")
    public Result<PageInfo<FoundItem>> getFoundItemsByPage(
            @RequestParam(defaultValue = "0") Integer categoryId,
            @RequestParam(defaultValue = "-1") Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String keyword) {

        // 调用服务层获取分页数据
        PageInfo<FoundItem> pageInfo = foundItemService.getFoundItemByPage(
                page, pageSize, categoryId, status, keyword);
        return Result.success(pageInfo);
    }

    /**
     * 处理添加寻物启事的请求
     *
     * @param foundItem 寻物启事的信息，包括物品描述、丢失地点等
     * @return 返回添加结果的字符串信息
     */
    @PostMapping("/add")
    public Result<String> addFoundItem(@RequestBody FoundItem foundItem) {
        // 设置创建时间和更新时间为当前时间
        foundItem.setCreateTime(new Date());
        foundItem.setUpdateTime(new Date());

        log.info("添加寻物启事：{}", foundItem);
        // 保存到数据库
        int result = foundItemService.addFoundItem(foundItem);

        // 根据保存结果返回相应的信息
        if (result == 1) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    /*更新寻找物品的信息*/
    @PutMapping("/update")
    public Result<String> updateFoundItem(@RequestBody FoundItem foundItem) {
        int result = foundItemService.updateFoundItem(foundItem);
        if (result == 1) {
            return Result.success();
        } else {
            return Result.error("更新失败");
        }
    }

    /*删除寻找物品*/
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteFoundItem(@PathVariable Integer id) {
        int result = foundItemService.deleteFoundItem(id);
        if (result == 1) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }

    /*根据id查询招领物品*/
    @GetMapping("/{id}")
    public Result<FoundItem> getFoundItemById(@PathVariable Integer id) {
        FoundItem foundItem = foundItemService.getFoundItemById(id);
        if (foundItem != null) {
            return Result.success(foundItem);
        } else {
            return Result.error("未找到该招领信息");
        }
    }

    //添加评论
    @PostMapping("/addComment")
    public Result<String> addComment(@RequestBody Comment comment) {
        int result = foundItemService.addComment(comment.getContent(), comment.getUserId(), comment.getItemId());
        if (result == 1) {
            return Result.success();
        } else {
            return Result.error("添加失败");
        }
    }

    //获取物品的所有评论
    @GetMapping("/getComments/{itemId}")
    public Result<List<Comment>> getComments(@PathVariable Integer itemId) {
        List<Comment> comments = foundItemService.getComments(itemId);
        log.info("获取物品的评论：{}", comments);
        if (comments != null) {
            return Result.success(comments);
        } else {
            return Result.error("未找到该物品的评论");
        }
    }
    //获取所有物品
    @GetMapping("/getAllPage")
    public Result<PageInfo<FoundItem>> getAllFoundItemsByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<FoundItem> pageInfo = foundItemService.getAllFoundItemsByPage(page, pageSize);
        return Result.success(pageInfo);
    }
    //更新状态
    @PutMapping("/approve/{id}")
    public Result<String> updateStatus(@PathVariable Integer id, @RequestParam(defaultValue = "2") String status) {
        //将字符转化为int
        int statusInt = Integer.parseInt(status);
        int result = foundItemService.updateStatus(id, statusInt);
        if (result == 1) {
            return Result.success();
        } else {
            return Result.error("更新失败");
        }
    }
    @PutMapping("/reject/{id}")
    public Result<String> reject(@PathVariable Integer id, @RequestParam(defaultValue = "5") String status) {
        //将字符转化为int
        int statusInt = Integer.parseInt(status);
        int result = foundItemService.updateStatus(id, statusInt);
        if (result == 1) {
            return Result.success();
        } else {
            return Result.error("更新失败");
        }
    }

    //获取所有物品审核记录
    @GetMapping("/getAllRecord")
    public Result<PageInfo<FoundItem>> getAllRecord(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<FoundItem> pageInfo = foundItemService.getAllRecord(page, pageSize);
        return Result.success(pageInfo);
    }

}
